package org.whuims.indritools.optmizing;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;

public class IndriExecuter {
	private String indriRTPath;
	private String queryContent;
	private Map<String, Double> scoreMap = new HashMap<String, Double>();

	/**
	 * 
	 * @param indriRTPath
	 *            IndriRunQuery.exe的地址。
	 * @param queryPath
	 *            交付IndriRunQuery
	 */
	public IndriExecuter(String indriRTPath, String queryContent) {
		super();
		this.indriRTPath = indriRTPath;
		this.queryContent = queryContent;
	}

	public Map<String, Double> analyze() {
		analyzeIt();
		return this.scoreMap;
	}

	private void analyzeIt() {
		File resultFile = this.getResultFile();
		String resultPath = resultFile.getAbsolutePath();
		File queryFile = this.getQueryFile();
		String queryPath = queryFile.getAbsolutePath();
		String command = "cmd /C " + this.indriRTPath + " " + queryPath + " > "
				+ resultPath;
		System.out.println(command);
		try {
			Process process = Runtime.getRuntime().exec(command);
			process.waitFor();
			analyzeRetDump(resultFile);
			this.clearFile(resultFile);
			this.clearFile(queryFile);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	private void analyzeRetDump(File resultFile) {
		String resulDump=FileUtils.readFileToString(resultFile);
		SingleFileEval eval=new SingleFileEval(resultFile.getAbsolutePath(),);
	}

	private File getQueryFile() {
		File queryFile = new File("test/queryFile.txt");
		try {
			FileUtils.write(queryFile, this.queryContent);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return queryFile;
	}

	/**
	 * 返回一个地址，用于存储中间文件。
	 * 
	 * @return
	 */
	private File getResultFile() {
		File file = new File("test/test1.txt");
		if (!file.getParentFile().exists()) {
			file.getParentFile().mkdirs();
		}
		return file;
	}

	private void clearFile(File file) {
		file.delete();
	}

	public static void main(String[] args) {
		IndriExecuter ie = new IndriExecuter(
				"D:\\Program\\Indri\\bin\\IndriRunQuery", "");
		ie.analyze();
	}
}
